// pages/shopList/shopList.js
import {
  HTTP
} from '../../utils/http-p.js'
const http = new HTTP;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cateoryTypeList: [],
    shopList:[],
    loading:false,
    bg: "",
    value: "",
    falge: true,
    items: [{
        // 导航名称
        text: '所有城市',
        // 禁用选项
        disabled: false,
        // 该导航下所有的可选项
      },
      {
        // 导航名称
        text: '所有城市',
        // 禁用选项
        disabled: false,
        // 该导航下所有的可选项
      }
    ],
    mainActiveIndex: 0,
    activeId: 1,
    active: 1,
    currentTab: 'all'
  },
  goToSearch: function() {
    wx.navigateTo({
      url: '/pages/searchShop/searchShop'
    })
  },
  goToShopInfo(event){
    const productId = event.currentTarget.dataset.productid
    wx.navigateTo({
      url: `/pages/shopInfo/shopInfo?productId=${productId}`
    })
  },
  onFakePost(e) {
    console.log(e)
    var index = e.target.dataset.current;
    console.log(index)
    if(index == 'all'){
      this.queryShopList('')
    }else{
      this.queryShopList(index)
    }
    if (this.data.currentTab == index) {
      return false;
    } else {
      this.setData({
        currentTab: index
      });
    }

  },

  /**
   * 查询分类
   */
  queryCateoryType() {
    http.request({
      url: 'product-category/getAll',
      success: (res) => {
        const list = res.data;
        console.log(list)
        list.unshift({
          "categoryId": "all",
          "categoryName": "全部",
          "categoryType": "all",              "categoryIcon":'https://jintai-1252143811.cos.ap-beijing.myqcloud.com/zpx-TIM%E5%9B%BE%E7%89%8720200413145928.png',
          "createTime": "2020-03-10 05:24:09",
          "updateTime": "2020-03-11 10:55:59"
        })
        this.setData({
          cateoryTypeList: list
        })
      }
    })  
  },

  /**
   * 查询商品
   */
  queryShopList(categoryType){
    this.setData({
      loading: true
    })
    http.request({
      url: 'product-info/getAll',
      data:{
        categoryType: categoryType
      },
      success: (res) => {
        
        const list = res.data;
        console.log(list)
        this.setData({
          shopList: list,
          loading:false
        })
        console.log(JSON.stringify(list))
        
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.queryCateoryType()
    this.queryShopList('')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})